package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private String username;
    private String password;
    private String email;
    private String firstname;
    private String lastname;
    private String status;
    private String addr1;
    private String addr2;
    private String state;
    private String phone;
    private String zip;
    private String country;
    private String city;
    private String verifycode;
    private String favouriteCategoryId;//宠物偏好
    private String languagePreference;//语言偏好
    private String listOption;
    private String bannerOption;
    private static final String REGISTER_FORM = "/WEB-INF/jsp/account/login&register.jsp";
//    private static final String REGISTER_FORM = "/WEB-INF/jsp/account/register.jsp";

    private String Msg;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.username = req.getParameter("username");
        this.password = req.getParameter("password");

//        this.email = req.getParameter("email");
//        this.firstname = req.getParameter("firstname");
//        this.lastname = req.getParameter("lastname");
//        this.addr1 = req.getParameter("add1");
//        this.state = req.getParameter("state");
//        this.phone = req.getParameter("phone");
//        this.zip = req.getParameter("zip");
//        this.country = req.getParameter("country");
//        this.city = req.getParameter("state");
//        this.verifycode = req.getParameter("vcode");
//        this.status = "OK";
//        this.languagePreference = "English";
//        this.listOption = req.getParameter("listOption");
//        this.bannerOption = req.getParameter("bannerOption");
//        this.favouriteCategoryId = req.getParameter("favouriteCategoryId");

        this.email = "NULL";
        this.firstname = "NULL";
        this.lastname = "NULL";
        this.addr1 = "NULL";
        this.state = "NULL";
        this.phone = "NULL";
        this.zip = "NULL";
        this.country = "NULL";
        this.city = "NULL";
        this.verifycode = "NULL";
        this.status = "OK";
        this.languagePreference = "English";
        this.listOption = "1";
        this.bannerOption = "1";
        this.favouriteCategoryId = "CATS";

        if(req.getParameter("add2") == null || !(req.getParameter("add2").equals(""))){
            this.addr2 = "NULL";
        }else{
            this.addr2 = req.getParameter("add2");
        }

//        if(req.getParameter("languagePreference") == null || !(req.getParameter("languagePreference").equals(""))){
//            this.languagePreference = "";
//        }else{
//            this.languagePreference = req.getParameter("languagePreference");
//        }


//        if(req.getParameter("bannerName") == null || !(req.getParameter("bannerName").equals(""))){
//            this.languagePreference = "";
//        }else{
//            this.languagePreference = req.getParameter("bannerName");
//        }

        AccountService accountService = new AccountService();
//        String exc=req.getParameter("vcode");//获取前端用户填写的验证码
        HttpSession session =req.getSession();  //获取session
//        String excode=(String) session.getAttribute("session_code");  //获取后端checkcode随机验证码
//        //为防止验证码重复使用，session中的session_code一旦获得，就必须删除
//        session.removeAttribute("session_code");


            //建立新的账户
            System.out.println(bannerOption);
            accountService.createAccount(username,password,email,firstname,lastname,status,addr1,addr2,state,phone,zip,country,city,favouriteCategoryId,listOption,bannerOption);

            resp.sendRedirect("loginRegister");



    }
    private boolean validate(String exc,String excode){
        if(this.username == null || this.username.equals("")){
            this.Msg = "用户名不能为空";
            return false;
        }
        if(this.password == null || this.password.equals("")){
            this.Msg = "密码不能为空";
            return false;
        }
        if(this.email == null || this.email.equals("")){
            this.Msg = "请输入您的email";
            return false;
        }
        if(this.firstname == null || this.firstname.equals("")){
            this.Msg = "请输入您的名字";
            return false;
        }
        if(this.lastname == null || this.lastname.equals("")){
            this.Msg = "请输入您的姓氏";
            return false;
        }
        if(this.addr1 == null || this.addr1.equals("")){
            this.Msg = "请输入您的地址";
            return false;
        }
        if(this.state == null || this.state.equals("")) {
            this.Msg = "请输入您所在省、市或直辖区";
            return false;
        }
        if(this.phone == null || this.phone.equals("")) {
            this.Msg = "请输入您的电话";
            return false;
        }
        if(this.zip == null || this.zip.equals("")) {
            this.Msg = "请输入邮政编码";
            return false;
        }
        if(this.country == null || this.country.equals("")){
            this.Msg = "请输入您所在国家";
            return false;
        }
        if(this.city == null || this.city.equals("")){
            this.Msg = "请输入您所在城市";
            return false;
        }
        if(exc == null || exc.equals("")){
            this.Msg = "请输入验证码";
            return false;
        }
        if(excode==null || !(excode.equalsIgnoreCase(exc))) {
            //忽略字母大小写，比较验证码
            //如果验证码正确，再比较用户的用户名和密码
            //验证码正确
            this.Msg = "验证码输入错误，请重新输入";
            return false;
        }
        return true;
    }
}


